package com.chatsystem.controller;

import com.chatsystem.dto.ApiResponse;
import com.chatsystem.dto.UserUpdateDto;
import com.chatsystem.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

/**
 * 用户控制器
 * 
 * @author ChatSystem
 * @version 1.0.0
 */
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class UserController {

    private final UserService userService;

    /**
     * 获取所有用户
     * 
     * @return 用户列表
     */
    @GetMapping
    public ResponseEntity<ApiResponse<?>> getAllUsers() {
        try {
            var users = userService.getAllUsers();
            return ResponseEntity.ok(ApiResponse.success(users));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(ApiResponse.error(e.getMessage()));
        }
    }

    /**
     * 搜索用户
     * 
     * @param keyword 搜索关键词
     * @return 用户列表
     */
    @GetMapping("/search")
    public ResponseEntity<ApiResponse<?>> searchUsers(@RequestParam String keyword) {
        try {
            var users = userService.searchUsers(keyword);
            return ResponseEntity.ok(ApiResponse.success(users));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(ApiResponse.error(e.getMessage()));
        }
    }

    /**
     * 根据ID获取用户信息
     * 
     * @param id 用户ID
     * @return 用户信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<?>> getUserById(@PathVariable Long id) {
        try {
            var userOpt = userService.findById(id);
            if (userOpt.isPresent()) {
                return ResponseEntity.ok(ApiResponse.success(userOpt.get()));
            } else {
                return ResponseEntity.badRequest().body(ApiResponse.error("用户不存在"));
            }
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(ApiResponse.error(e.getMessage()));
        }
    }

    /**
     * 获取当前登录用户的个人资料
     *
     * @return 当前用户资料
     */
    @GetMapping("/me")
    public ResponseEntity<ApiResponse<?>> getCurrentUser() {
        try {
            String username = SecurityContextHolder.getContext().getAuthentication().getName();
            System.out.println("username = " + username);
            var userOpt = userService.getCurrentUserProfile(username);
            if (userOpt.isPresent()) {
                return ResponseEntity.ok(ApiResponse.success(userOpt.get()));
            } else {
                return ResponseEntity.badRequest().body(ApiResponse.error("用户未登录或不存在"));
            }
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(ApiResponse.error(e.getMessage()));
        }
    }

    /**
     * 更新当前登录用户的个人资料
     *
     * @param userUpdateDto 包含昵称和头像URL的DTO
     * @return 更新后的用户资料
     */
    @PutMapping("/me")
    public ResponseEntity<ApiResponse<?>> updateCurrentUser(@RequestBody UserUpdateDto userUpdateDto) {
        System.out.println("userUpdateDto = " + userUpdateDto);
        try {
            String username = SecurityContextHolder.getContext().getAuthentication().getName();
            var updatedUser = userService.updateUserProfile(username, userUpdateDto.getNickname(), userUpdateDto.getAvatarUrl());
            return ResponseEntity.ok(ApiResponse.success(updatedUser));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(ApiResponse.error(e.getMessage()));
        }
    }
} 